#include "ADC.h"

uint16_t g_AdcDmaCacheBuffer[ADC_CHANNEL_COUNT] = {0};
AdcValue_t adcAnalyseBuffer[ADC_CHANNEL_COUNT] = {0};
void adcAverageFilter(void);
uint16_t u16AdcGetAverageValue(uint8_t u8Index);
/*
初始化步骤：
1.开ADC时钟
2.开TIM14时钟，配置TIM14以64M周期计数
3.打开电压调节器
4.延时100us
5.开启ADC校准
6.等待校准结束
7.通过CFGR1、CFGR2配置ADC(模式、周期、分辨率、对齐、序列)
8.等待配置生效
9.使能ADC
10.等待ADC准备完成
11.开始转换
12.配置DMA为循环模式
13.使能DMA请求
*/
/*
    PA0 A       CH0
    PA1 B       CH1
    PA2 C       CH2
    PA3 AVBUS   CH3
    PA4 AIBUS   CH4    
    PA5 APOT    CH5     
    PB1 ANTC    CH9    
*/
void ADC_Init(void)
{
    RCC->APBENR2 |= 0x00108000;

    TIM14->ARR = 65535;
    TIM14->CNT = 0;
    TIM14->CR1 |= 0x00000001;

    ADC1->CR |= 0x10000000;
    TIM14->CNT = 0;
    while( (TIM14->CNT) < 6400 );
    TIM14->CR1 &=~ 0x00000001;

    ADC1->CR |= 0x80000000;
    while( 0 != (ADC1->CR & 0x80000000) );
    
    ADC1->CFGR1 |= 0x00201400;
    ADC1->CFGR2 |= 0x40000000;

    ADC1->CHSELR = 0xF9543210;

    while( 0 == (ADC1->ISR & 0x00002000) );

    ADC1->CR |= 0x00000001;

    while( 0 == (ADC1->ISR & 0x00000001) );

    ADC1->CFGR1 |= 0x00000003;

    ADC1->CR |= 0x00000004;
}

void adcDMAFinishCallBack(void)
{
  /*更新瞬时�?*/
  for( uint8_t i = 0; i<ADC_CHANNEL_COUNT; i++ )
  {
    adcAnalyseBuffer[i].valueInstance = g_AdcDmaCacheBuffer[i];
  }
  adcAverageFilter();//更新平均�?
}

void adcAverageFilter(void)//平均值滤�?
{
  //滤波vbus
  adcAnalyseBuffer[AN_DEF_VBUS].valueSum += adcAnalyseBuffer[AN_DEF_VBUS].valueInstance;
  adcAnalyseBuffer[AN_DEF_VBUS].valueCnt++;

  if( adcAnalyseBuffer[AN_DEF_VBUS].valueCnt>=128 ){
    adcAnalyseBuffer[AN_DEF_VBUS].valueAverage = adcAnalyseBuffer[AN_DEF_VBUS].valueSum>>7;//移位代替除法
    adcAnalyseBuffer[AN_DEF_VBUS].valueSum = 0;
    adcAnalyseBuffer[AN_DEF_VBUS].valueCnt = 0;
  }

  //滤波IBUS
  adcAnalyseBuffer[AN_DEF_IBUS].valueSum += adcAnalyseBuffer[AN_DEF_IBUS].valueInstance;
  adcAnalyseBuffer[AN_DEF_IBUS].valueCnt++;

  if( adcAnalyseBuffer[AN_DEF_IBUS].valueCnt>=128 ){
    adcAnalyseBuffer[AN_DEF_IBUS].valueAverage = adcAnalyseBuffer[AN_DEF_IBUS].valueSum>>7;//移位代替除法
    adcAnalyseBuffer[AN_DEF_IBUS].valueSum = 0;
    adcAnalyseBuffer[AN_DEF_IBUS].valueCnt = 0;
  }  

  //滤波POT
  adcAnalyseBuffer[AN_DEF_POT].valueSum += adcAnalyseBuffer[AN_DEF_POT].valueInstance;
  adcAnalyseBuffer[AN_DEF_POT].valueCnt++;

  if( adcAnalyseBuffer[AN_DEF_POT].valueCnt>=128 ){
    adcAnalyseBuffer[AN_DEF_POT].valueAverage = adcAnalyseBuffer[AN_DEF_POT].valueSum>>7;//移位代替除法
    adcAnalyseBuffer[AN_DEF_POT].valueSum = 0;
    adcAnalyseBuffer[AN_DEF_POT].valueCnt = 0;
  }  
  //滤波NTC
  adcAnalyseBuffer[AN_DEF_NTC].valueSum += adcAnalyseBuffer[AN_DEF_NTC].valueInstance;
  adcAnalyseBuffer[AN_DEF_NTC].valueCnt++;

  if( adcAnalyseBuffer[AN_DEF_NTC].valueCnt>=64 ){
    adcAnalyseBuffer[AN_DEF_NTC].valueAverage = adcAnalyseBuffer[AN_DEF_NTC].valueSum>>6;//移位代替除法
    adcAnalyseBuffer[AN_DEF_NTC].valueSum = 0;
    adcAnalyseBuffer[AN_DEF_NTC].valueCnt = 0;
  }

}

uint16_t u16AdcGetAverageValue(uint8_t u8Index)
{
  uint16_t retval = 0;
  if(u8Index<ADC_CHANNEL_COUNT){
    retval = adcAnalyseBuffer[u8Index].valueAverage;
  }
  return retval;
}
